package com.romain.risotto;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.util.Log;
import android.widget.Toast;

import com.romain.risotto.entities.Ship;
import com.romain.risotto.manager.GameManager;
import com.romain.risotto.message.MessageFlags;
import com.romain.risotto.texture.Textures;
import static org.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
public class MainActivity extends SimpleBaseGameActivity implements MessageFlags
{

	
	// ===========================================================
	// Fields
	// ===========================================================

	private SmoothCamera mCamera;
	private GameManager gameManager=null;
	
	

	private static final short FLAG_MESSAGE_SERVER_ADD_FACE = 1;
	private static final short FLAG_MESSAGE_SERVER_MOVE_FACE = FLAG_MESSAGE_SERVER_ADD_FACE + 1;

	
	
	@Override
	protected  void onResume() {
		RisottoApp app = (RisottoApp) getApplication();
		if(app.getConnection()==null){
			app.launchDiscoverServer();
		}
		super.onResume();
	}
	
	
	

	@Override
	public EngineOptions onCreateEngineOptions() {
		mCamera = new SmoothCamera(0, 0, Constantes.CAMERA_WIDTH, Constantes.CAMERA_HEIGHT, Constantes.SHIP_MAX_SPEED*PIXEL_TO_METER_RATIO_DEFAULT, Constantes.SHIP_MAX_SPEED*PIXEL_TO_METER_RATIO_DEFAULT, 0.2f);
		mCamera.setBounds(0,Constantes.FIELD_WIDTH, 0, Constantes.FIELD_HEIGHT);

		final EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
						Constantes.CAMERA_WIDTH, Constantes.CAMERA_HEIGHT), mCamera);
		engineOptions.getTouchOptions().setNeedsMultiTouch(true);
		
		
		return engineOptions;
	}

	@Override
	public void onCreateResources() {

		Textures.load(this.getTextureManager(), this);

		
	}

	@Override
	public Scene onCreateScene() {
		final Scene scene = new Scene();
		gameManager = GameManager.getInstance();
		gameManager.init(mEngine, scene ,mCamera,this.getVertexBufferObjectManager());
		scene.setTouchAreaBindingOnActionDownEnabled(true);
		((RisottoApp)getApplication()).setGameManager(gameManager);
		return scene;
	}
	private void toast(final String pMessage) {
		this.log(pMessage);
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(MainActivity.this, pMessage, Toast.LENGTH_SHORT).show();
			}
		});
	}
	private void log(final String pMessage) {
		Debug.d(pMessage);
	}
	
	
	
	
	


	
	
	
	

	
	

	
}
